<template>
  <div id="testadd">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/project/list' }">干部考核</el-breadcrumb-item>
      <el-breadcrumb-item>导入考核项目</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs" :height="tableHeight">
      <el-form :model="testAddForm" label-width="200px" :rules="testAddFormRules" ref="testAddForm" :inline="true" style="padding:20px">
        <el-form-item label="考核年度" prop="theYear">
          <el-select v-model="testAddForm.theYear" placeholder="请选择">
            <el-option v-for="(item, index) in yearlist" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位编码" prop="theYear" v-if="this.$route.params.id!=='0'">
          {{testAddForm.code}}
        </el-form-item>
        <el-form-item label="单位编码" prop="theYear" v-else>
          单位编码自动生成
        </el-form-item>
        <fieldset>
          <legend>被考核对象基本信息</legend>
        <el-form-item label="正职（主要）领导数" prop="leaderZhengNum" label-width="167px">
          <el-input-number v-model="testAddForm.leaderZhengNum" :min="0" :max="10" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <el-form-item label="副职领导数" prop="leaderFuNum">
          <el-input-number v-model="testAddForm.leaderFuNum" :min="0" :max="40" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <el-form-item label="提拔干部人数" prop="prefermentnum">
          <el-input-number v-model="testAddForm.prefermentnum" :min="0" :max="100" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <div style="clear:both;width:100%"></div>
        </fieldset>
        <fieldset>
          <legend>年度考核票准备</legend>
        <el-form-item label="单位主要领导票数" prop="ticketMainleadNum" label-width="167px">
          <el-input-number v-model="testAddForm.ticketMainleadNum" :min="0" :max="20" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <el-form-item label="其他干部票数" prop="ticketOtherNum">
          <el-input-number v-model="testAddForm.ticketOtherNum" :min="0" :max="1000" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <div style="width:100%;clear:both"></div>
        <el-form-item label="首页提示语" prop="welcome" label-width="167px">
          <el-input type="textarea" :rows="5" v-model="testAddForm.welcome" auto-complete="off" style="width:617px;"></el-input>
        </el-form-item>
        </fieldset>
        <fieldset>
          <legend>“一报告两评议”票准备</legend>
        <el-form-item label="A类选票数" prop="ticketAtypeNum" label-width="167px">
          <el-input-number v-model="testAddForm.ticketAtypeNum" :min="0" :max="80" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <el-form-item label="B类选票数" prop="ticketBtypeNum">
          <el-input-number v-model="testAddForm.ticketBtypeNum" :min="0" :max="1000" auto-complete="off" style="width:217px;"></el-input-number>
        </el-form-item>
        <div style="width:100%;clear:both"></div>
        <el-form-item label="首页提示语" prop="welcome2" label-width="167px">
          <el-input type="textarea" :rows="5" v-model="testAddForm.welcome2" auto-complete="off" style="width:617px;"></el-input>
        </el-form-item>
        </fieldset>
        <div style="clear:both;width:100%"></div>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="3" v-model="testAddForm.remark" auto-complete="off" style="width:617px;"></el-input>
        </el-form-item>
        <div style="clear:both;width:100%"></div>
        <el-button type="primary" @click.native="testAddFormSubmit" :loading="testAddFormLoading" style="margin-left:200px">保存</el-button><span style="color:#F56C6C;font-size:14px;padding:10px;">{{errortext}}</span>
      </el-form>
    </section>
  </div>
</template>

<script>
export default {
  data () {
    var validateTicketAtypeNum = (rule, value, callback) => {
      if (value + this.testAddForm.ticketBtypeNum > 0) {
        if (value < this.testAddForm.leaderZhengNum + this.testAddForm.leaderFuNum) {
          callback(new Error('A类选票数 不能小于 单位领导数'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validateTicketMainleadNum = (rule, value, callback) => {
      if (value + this.testAddForm.ticketOtherNum > 0) {
        if (value < this.testAddForm.leaderZhengNum) {
          callback(new Error('单位主要领导票数 不能小于 正职（主要）领导数'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validateTicketOtherNum = (rule, value, callback) => {
      if (value + this.testAddForm.ticketMainleadNum > 0) {
        if (value < this.testAddForm.leaderFuNum) {
          callback(new Error('其他干部票数 不能小于 副职领导数'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validateprefermentnum = (rule, value, callback) => {
      if (this.testAddForm.ticketAtypeNum + this.testAddForm.ticketBtypeNum > 0) {
        if (value === 0) {
          callback(new Error('如果有“一报告两评议”选票时，提拔干部人数不能为0'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validatewelcome = (rule, value, callback) => {
      if (this.testAddForm.ticketMainleadNum + this.testAddForm.ticketOtherNum > 0) {
        if (value === '' || value === null) {
          callback(new Error('如果有年度考核选票时，年度考核首页提示语不能为空'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validatewelcome2 = (rule, value, callback) => {
      if (this.testAddForm.ticketAtypeNum + this.testAddForm.ticketBtypeNum > 0) {
        if (value === '' || value === null) {
          callback(new Error('如果有“一报告两评议”选票时，“一报告两评议”首页提示语不能为空'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      errortext: '',
      yearlist: [],
      testAddFormVisible: false,
      testAddFormLoading: false,
      testAddForm: {
        theYear: '',
        leaderZhengNum: 0,
        leaderFuNum: 0,
        ticketMainleadNum: 0,
        ticketOtherNum: 0,
        ticketAtypeNum: 0,
        ticketBtypeNum: 0,
        prefermentnum: 0,
        welcome: '',
        welcome2: '',
        remark: ''
      },
      testAddFormRules: {
        // theYear: [
        //   { required: true, message: '请输入年度', trigger: 'blur' }
        // ],
        welcome: [
          { validator: validatewelcome, trigger: 'blur' },
          { max: 200, message: '首页提示语不能超过200字', trigger: 'blur' }
        ],
        welcome2: [
          { validator: validatewelcome2, trigger: 'blur' },
          { max: 200, message: '首页提示语不能超过200字', trigger: 'blur' }
        ],
        remark: [
          { max: 100, message: '备注不能超过100字', trigger: 'blur' }
        ],
        ticketAtypeNum: [
          { validator: validateTicketAtypeNum, trigger: 'blur' }
        ],
        ticketMainleadNum: [
          { validator: validateTicketMainleadNum, trigger: 'blur' }
        ],
        ticketOtherNum: [
          { validator: validateTicketOtherNum, trigger: 'blur' }
        ],
        prefermentnum: [
          { validator: validateprefermentnum, trigger: 'blur' }
        ]
      },
      total: 0,
      pageNum: 1,
      pageSize: 20,
      listLoading: false,
      totalnum: 0,
      Isace: true,
      Sortkey: '',
      changeSizeFlag: false,
      tableHeight: 100
    }
  },
  methods: {
    getPrjDetails (id) {
      var _this = this
      this.listLoading = true
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      this.$http
        .get(`/api/prj/details/` + id)
        .then(function (response) {
          _this.testAddForm = response.data.project
          console.log('项目详情：')
          console.log(_this.testAddForm)
          _this.yearlist = response.data.dicyear
        })
        .catch(function (error) {
          _this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    testAddFormSubmit: function () {
      var _this = this
      this.$refs.testAddForm.validate((valid) => {
        if (valid) {
          if (this.testAddForm.leaderZhengNum + this.testAddForm.leaderFuNum === 0) {
            _this.errortext = '被考核对象总数不能为0'
          } else {
            if (this.testAddForm.ticketMainleadNum + this.testAddForm.ticketOtherNum + this.testAddForm.ticketAtypeNum + this.testAddForm.ticketBtypeNum === 0) {
              _this.errortext = '总票数不能为0'
            } else {
              _this.errortext = ''
              _this.testAddFormLoading = true
              let para = {
                id: this.$route.params.id,
                the_year: this.testAddForm.theYear,
                leader_zheng_num: this.testAddForm.leaderZhengNum,
                leader_fu_num: this.testAddForm.leaderFuNum,
                prefermentnum: this.testAddForm.prefermentnum,
                ticket_mainlead_num: this.testAddForm.ticketMainleadNum,
                ticket_other_num: this.testAddForm.ticketOtherNum,
                ticket_atype_num: this.testAddForm.ticketAtypeNum,
                ticket_btype_num: this.testAddForm.ticketBtypeNum,
                welcome: this.testAddForm.welcome,
                welcome2: this.testAddForm.welcome2,
                remark: this.testAddForm.remark
              }
              this.$http.post(`/api/prj/edit`, para).then(function (response) {
                console.log(response.data.IsSuccess)
                if (response.data.IsSuccess === true) {
                  _this.$message({
                    message: '保存成功',
                    type: 'success'
                  })
                  _this.$refs['testAddForm'].resetFields()
                  _this.testAddFormVisible = false
                  _this.testAddFormLoading = false
                  _this.$router.push('/project/list/')
                } else {
                  console.log('保存失败:' + response.data.message)
                  _this.$message({
                    message: '保存失败:' + response.data.message,
                    type: 'error'
                  })
                  _this.testAddFormLoading = false
                }
              }).catch(function (error) {
                console.log(error)
                _this.$message({
                  message: '保存失败:' + error,
                  type: 'error'
                })
                _this.testAddFormLoading = false
              })
            }
          }
        }
      })
    }
  },
  mounted () {
    this.getPrjDetails(this.$route.params.id)
  }
}
</script>
<style>
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testadd .wboxs{
    padding-top:10px;
  }
</style>
<style scoped>
fieldset{
  margin:20px;
  padding-top:20px;
  border-width: 1px;
  border-bottom-color: #ccc;
}
legend{
  padding: 0 10px;
  font-weight: bolder;
  font-size:var(--size16) !important;
}
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
